package com.ruoyi.cpsp.service.impl;
/*
    @author neu.xiangwenjuan
    @create 2023/3/14
*/

import com.ruoyi.cpsp.domain.CurrentPerformanceData;
import com.ruoyi.cpsp.domain.CurrentPerformanceDataEn;
import com.ruoyi.cpsp.domain.CurrentPerformanceTotal;
import com.ruoyi.cpsp.domain.EmployeeEntity;
import com.ruoyi.cpsp.mapper.CurrentPerformanceDataMapper;
import com.ruoyi.cpsp.mapper.EmployeeMapper;
import com.ruoyi.cpsp.service.CurrentPerformanceDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CurrentPerformanceDataServiceImpl implements CurrentPerformanceDataService {

    @Autowired
    private CurrentPerformanceDataMapper mapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public int insert(CurrentPerformanceData currentPerformanceData) {
        return mapper.insert(currentPerformanceData);
    }

    @Override
    public int updateByPrimaryKeySelective(CurrentPerformanceData record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<CurrentPerformanceDataEn> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public List<CurrentPerformanceData> getAllInfo(Integer state) {
        return mapper.getAllInfo(state);
    }

    @Override
    public List<CurrentPerformanceDataEn> selectDataByIdCard(List<String> idCardList) {
        return mapper.selectDataByIdCard(idCardList);
    }

    @Override
    public List<CurrentPerformanceDataEn> selectSelective(String department, Integer teamGroup, String employeeInfo) {
        return mapper.selectSelective(department, teamGroup, employeeInfo);
    }

    @Override
    public CurrentPerformanceTotal selectDataTotal(String departmentId, Integer teamGroupId) {
        return mapper.selectDataTotal(departmentId, teamGroupId);
    }

    @Override
    public double selectSum(Integer year, Integer quarter) {
        return mapper.selectSum(year, quarter);
    }

    @Override
    public double selectSumDepart(List<String> idCardList, Integer year, Integer quarter) {
        return mapper.selectSumDepart(idCardList, year, quarter);
    }

    @Override
    public int selectCountByQuarter(Integer year, Integer quarter) {
        return mapper.selectCountByQuarter(year, quarter);
    }

    @Override
    public CurrentPerformanceData selectDataByEmployee(Integer year, Integer quarter, String employeeIdCard) {
        return mapper.selectDataByEmployee(year, quarter, employeeIdCard);
    }

    @Override
    public int insertPerformance() {
        System.out.println("enter insert------------------------");
        List<EmployeeEntity> eml = new ArrayList<EmployeeEntity>();
        eml = employeeMapper.selectAll();

        CurrentPerformanceData cp = new CurrentPerformanceData();
        cp.setYear(2023);
        cp.setQuarter(3);
        cp.setCumulativeBase(200.00);

        for (int i = 0; i < eml.size(); i++) {
            String idCard = eml.get(i).getIdCard();
            cp.setEmployeeIdCard(idCard);
            if (mapper.selectSelective("",0,idCard).size() == 0) {
                if(employeeMapper.getIsDirectProducer(idCard)){
                    cp.setIncreaseBase(100.00);
                }else{
                    cp.setIncreaseBase(50.00);
                }
                double cumulativeBase = cp.getCumulativeBase();
                double increaseBase = cp.getIncreaseBase();
                cp.setCurrentBase(cumulativeBase + increaseBase);
                cp.setMoneyEveryMonth(cumulativeBase + increaseBase);
                cp.setMoneyQuarter((cumulativeBase + increaseBase) * 3);
                int flag = mapper.insert(cp);
                if(flag == 0){
                    return 0;
                }
            }
        }
        return 1;

    }

    @Override
    public int deleteCurrentPerformanceData() {
        return mapper.deleteCurrentPerformanceData();
    }

    @Override
    public int updateCurrentAnnotation(List<String> idCardList) {
        return mapper.updateCurrentAnnotation(idCardList);
    }


}
